unit Heap_Vis;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Math, StdCtrls;

const
  H= 20;
  L = 500;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mass: array of integer;
  mass1: array of TPanel;
  arr: array of TPanel;
  n, i, m, p, j, t :integer;
  fin: textfile;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer2.Enabled:=false;
  Timer1.Enabled:=false;
  AssignFile (fin, 'input.txt');
  Reset (fin);
  readln (fin, n);
  setlength (mass, n);
  for i:= 0 to n-1 do
    read (fin, mass[i]);
  setlength (mass1, n);
  for i:= 0 to n-1 do
  begin
    mass1[i]:=TPanel.Create(Form1);
    mass1[i].Parent:=Form1;
    mass1[i].Height:=H;
    mass1[i].Width:=H;
    mass1[i].Top:=10+trunc(Log2(i+1))*(10+H);
    mass1[i].Color:=clWhite;
    mass1[i].Caption:=IntToStr(mass[i]);
    p:=1;
    for j:=1 to trunc(log2(i+1)) do
      p:=p*2;
    mass1[i].Left:=(L div (p+1))*((i+1) mod p + 1) - h div 2;
  end;
  setlength (arr, n);
  for i:= 0 to n-1 do
  begin
    arr[i]:=TPanel.Create(Form1);
    arr[i].Parent:=Form1;
    arr[i].Color:=clWhite;
    arr[i].Height:=2*h;
    arr[i].Width:=2*h;
    arr[i].Top:=Form1.Height-4*h;
    arr[i].Left:= i*2*h;
  end;
  t:=n-1;
  closefile (fin);
  Timer1.Enabled:=true;
end;

procedure Heapify (p, m: integer);
var a,b: integer;
begin
  if (m=1) then
    Form1.Timer1.Enabled:=false;
  if (m=2) then
    Form1.Timer2.Enabled:=false;
  while (1=1) do
  begin
    a:=2000000;
    b:=2000000;
    if (2*p+1<n) then
      a:=mass[2*p+1];
    if (2*p+2<n) then
      b:=mass[2*p+2];
    if (a<=b) and (a<mass[p]) then
    begin
      mass[p]:=mass[p]+mass[2*p+1];
      mass[2*p+1]:=mass[p]-mass[2*p+1];
      mass[p]:=mass[p]-mass[2*p+1];
      p:=p*2+1;
    end
    else
    begin
      if (b<a) and (b<mass[p]) then
      begin
        mass[p]:=mass[p]+mass[2*p+2];
        mass[2*p+2]:=mass[p]-mass[2*p+2];
        mass[p]:=mass[p]-mass[2*p+2];
        p:=2*p+2;
      end
      else
        break;
    end;
    for i:= 0 to n-1 do
      mass1[i].Caption:=IntToStr(mass[i]);
  end;
  if (m=1) then
    Form1.Timer1.Enabled:=true;
  if (m=2) then
    Form1.Timer2.Enabled:=true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (t=-1) then
  begin
    Timer1.Enabled:=false;
    Timer2.Enabled:=true;
    m:=n;
    t:=1;
    exit;
  end;
  Heapify (t, 1);
  t:=t-1;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  if (t>m-1) then
  begin
    Timer2.Enabled:=false;
    n:=m;
    arr[t-1].Caption:=inttostr(mass[0]);
    exit;
  end;
  arr[t-1].Caption:=inttostr(mass[0]);
  mass[0]:=mass[n-1]+mass[0];
  mass[n-1]:=mass[0]-mass[n-1];
  mass[0]:=mass[0]-mass[n-1];
  for i:= 0 to n-1 do
    mass1[i].Caption:=IntToStr(mass[i]);
  n:=n-1;
  heapify (0, 2);
  t:=t+1;
end;


end.
